package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC121 {

    public int maxProfit(int[] prices) {
        if (null == prices || prices.length <= 1) {
            return 0;
        }

        int dp[] = new int[prices.length];
        Arrays.fill(dp, Integer.MAX_VALUE);

        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1] || prices[i] > dp[i - 1]) {
                dp[i] = dp[i - 1] < prices[i - 1] ? dp[i - 1] : prices[i - 1];
            }

            if (prices[i] - dp[i] > maxProfit) {
                maxProfit = prices[i] - dp[i];
            }
        }

        return maxProfit;
    }


    public static void main(String[] args) {
        LC121 an = new LC121();
        int arr[] = InputUtil.toIntegerArray("[7,1,5,3,6,4]");
        System.out.println(an.maxProfit(arr));
    }
}
